package com.studay;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.studay.bean.Const;
import com.util.LogManager;

public class FlashActivity extends Activity {

	private WebView mWebView;
	// private Button playButton, pauseButton, exitButton;
	private ProgressBar mProgressBarHorizontal;
	private final static int PROGRESSBARSIZE = 0x0000;
	private final static int FLASH_START = 0x0001;
	private String flashName;
	private boolean stopThread = false;
	private ProgressDialog mProgressDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.video);

		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.setIcon(android.R.drawable.ic_menu_info_details);
		mProgressDialog.setMessage("视频加载中,请稍候......");
		mProgressDialog.show();

		Intent intent = this.getIntent();
		Bundle bundle = intent.getBundleExtra("videoBundle");
		flashName = bundle.getString("fileName");
		String html = Const.videoHtml.replace(Const.test, flashName);
		LogManager.LogShow(Const.TAG, html, LogManager.INFO);
		mWebView = (WebView) findViewById(R.id.webView01);
		mProgressBarHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal);
		this.setProgress(mProgressBarHorizontal.getProgress() * 100);
		// playButton = (Button) findViewById(R.id.playButton);
		// pauseButton = (Button) findViewById(R.id.pauseButton);
		// exitButton = (Button) findViewById(R.id.exitButton);
		// playButton.setOnClickListener(buttonListener);
		// pauseButton.setOnClickListener(buttonListener);
		// exitButton.setOnClickListener(buttonListener);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.getSettings().setPluginState(PluginState.ON);
		mWebView.setWebChromeClient(new WebChromeClient());
//		mWebView.getSettings().setAllowFileAccess(true);
//		mWebView.getSettings().setPluginsEnabled(true);
//		mWebView.getSettings().setAppCacheEnabled(true);
		WebSettings mWebSetting = mWebView.getSettings();
		mWebSetting.setLoadWithOverviewMode(true);
		// mWebView.addJavascriptInterface(new CallJava(), "CallJava");
		// mWebView.loadUrl("file:///android_asset/demo/flashDemo.html");
		mWebView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
		startThread();
	}

	// Button.OnClickListener buttonListener = new Button.OnClickListener() {
	// @Override
	// public void onClick(View v) {
	// int buttonID = v.getId();
	// switch (buttonID) {
	// case R.id.playButton:
	// // mWebView.loadUrl("javascript:Play()");
	// showFlashProgress(5);
	// break;
	// case R.id.pauseButton:
	// // mWebView.loadUrl("javascript:Pause()");
	// break;
	// case R.id.exitButton:
	// quitDialog();
	// break;
	// default:
	// break;
	// }
	// }
	// };

	public void showFlashProgress(float progressSize) {
		int size = (int) progressSize;
		// Toast.makeText(this, size+"", Toast.LENGTH_SHORT).show();
		mProgressBarHorizontal.setProgress(size);
	}

	private void quitDialog() {
		new AlertDialog.Builder(this).setMessage("确认退出吗？").setPositiveButton(
				"确认", new AlertDialog.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						stopThread = true;
						FlashActivity.this.finish();
					}
				}).setNegativeButton("取消", null).show();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			quitDialog();
			break;
		default:
			break;
		}
		return false;
	}

	@Override
	protected void onPause() {
		super.onPause();
		mWebView.pauseTimers();
		if (isFinishing()) {
			mWebView.loadUrl("about:blank");
			setContentView(new FrameLayout(this));
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		mWebView.resumeTimers();
	}

	// private final class CallJava{
	// public void consoleFlashProgress(float progressSize){
	// showFlashProgress(progressSize);
	// }
	// }

	private void startThread() {
		// 通过线程来改变ProgressBar的值
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(1000);
					Message message = new Message();
					message.what = FlashActivity.FLASH_START;
					FlashActivity.this.myMessageHandler.sendMessage(message);
				} catch (InterruptedException e1) {
					Thread.currentThread().interrupt();
				}

				while (!stopThread && !Thread.currentThread().isInterrupted()) {
					try {
						Thread.sleep(1000);
						Message message2 = new Message();
						message2.what = FlashActivity.PROGRESSBARSIZE;
						FlashActivity.this.myMessageHandler
								.sendMessage(message2);
					} catch (Exception e) {
						Thread.currentThread().interrupt();
					}
				}
			}
		}).start();
	}

	Handler myMessageHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case FlashActivity.PROGRESSBARSIZE:
				break;
			case FlashActivity.FLASH_START:
				LogManager.LogShow(Const.TAG, "play video : " + flashName,
						LogManager.INFO);
				mProgressDialog.dismiss();
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};
}